package server.gui;

import java.io.*;
import java.awt.*;
import javax.swing.*;

final public class MorcGUI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Container container;
	private JButton refreshScrapsButton;
	private JTextArea	textBox;
	private JLabel	 	eventsBox;
	private JScrollPane textBoxScroll;
	
	private BufferedReader bufferedReader;
	private File file = new File("Scraps.txt");

	public MorcGUI() {
		super("MobileOrkut Server");
		container = getContentPane();
		container.setLayout(new BorderLayout());
		eventsBox = new JLabel("Servindo...");
		refreshScrapsButton = new JButton("Refresh");
		refreshScrapsButton.addActionListener(new ScrapListener(this));
		textBox = new JTextArea();
		textBoxScroll = new JScrollPane(textBox);
		textBoxScroll.setPreferredSize(new Dimension(500, 150));
		textBoxScroll.setAutoscrolls(true);
		container.add(refreshScrapsButton, BorderLayout.NORTH);
		container.add(textBox, BorderLayout.CENTER);
		container.add(eventsBox, BorderLayout.SOUTH);
		setSize(500, 300);
		setVisible(true);
	}

	public static void main(String ignoreArgs[]) {
		MorcGUI application = new MorcGUI();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void refreshScrapbox() {
		String s = "";
		textBox.setText(s);
		try {			
			new StatusChangeTimer(this, 2, "Atualizando scrapbox...");
			bufferedReader = new BufferedReader(new FileReader(file));
			while ( (s = bufferedReader.readLine()) != null) {
				textBox.append(s + "\n");
			}
			bufferedReader.close();
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	public void setEvent(String msg) { this.eventsBox.setText(msg); }
	
}
